<template>
  <div class="postManagement">
    <el-card class="box-card">
      <el-row>
        <el-form>
          <el-col :span="12">
            <el-form-item>
              <el-input
                v-model="queryData.name"
                placeholder="岗位名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-button @click="handlerSearch">查询</el-button>
            <el-button type="primary" @click="handlerAddPost"
              >新增岗位</el-button
            >
          </el-col>
        </el-form>
      </el-row>
    </el-card>
    <el-card class="box-card">
      <el-table v-loading="loading" border :data="postList" style="width: 100%">
        <transition
          name="animate__animated animate__bounce"
          enter-active-class="animate__swing"
          leave-active-class="animate__rotateOutDownLeft"
          :appear="true"
        >
          <el-table-column align="center" prop="stationName" label="岗位名称">
          </el-table-column>
        </transition>
        <transition
          name="animate__animated animate__bounce"
          enter-active-class="animate__swing"
          leave-active-class="animate__rotateOutDownLeft"
          :appear="true"
        >
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="handlerEditBtn(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button type="text" size="small" @click="deletePost(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </transition>
      </el-table>
      <!-- 分页器 -->
      <Pagination
        :total="total"
        :page.sync="queryData.pageNum"
        :limit.sync="queryData.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加弹窗 -->
    <AddPost
      :addDialogVisible="addDialogVisible"
      @postCancel="postCancel"
      @addPost="addPost"
    />
    <!-- 修改弹窗 -->
    <EditPost
      :editDialogVisible="editDialogVisible"
      :editForm="editForm"
      @editPostCancel="editPostCancel"
      @editAddPost="editAddPost"
    />
  </div>
</template>
<script>
import AddPost from "./components/addPost.vue";
import EditPost from "./components/editPost.vue";
import Pagination from "../../components//Pagination/index.vue";

//引入岗位管理接口
import { getPost, addPost, deletePost, updatePost } from "@/api/position";
export default {
  components: {
    AddPost,
    EditPost,
    Pagination,
  },
  data() {
    return {
      loading: true,
      queryData: {
        pageNum: 1, //当前显示多少数据
        pageSize: 8, // 当前页
        name: "", //角色名称
      },
      total: 0, // 总条数
      addDialogVisible: false,
      editDialogVisible: false,
      postList: [],
      editForm: { sname: "", id: "" },
      serch: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //获取表格数据
    getList() {
      getPost(this.queryData).then((res) => {
        if (res.status === 200) {
          this.loading = false;
          this.postList = res.data.records;
          this.total = res.data.total; //总条数
          this.queryData.pageNum = res.data.current; //当前页
          this.queryData.pageSize = res.data.size; //每页显示条数
        }
      });
    },
    // 查询按钮
    handlerSearch() {
      this.getList();
    },
    // 打开新增弹窗按钮
    handlerAddPost() {
      this.addDialogVisible = true;
    },
    // 取消新增按钮
    postCancel(ValFalse) {
      this.addDialogVisible = ValFalse;
    },
    // 确认新增按钮
    addPost(valList, ValFalse) {
      addPost(valList).then((res) => {
        if (res.status === 200) {
          this.getList();
          this.$message({
            message: "新增成功",
            type: "success",
          });
        }
      });

      this.addDialogVisible = ValFalse;
    },
    // 打开修改弹窗按钮
    handlerEditBtn(index, row) {
      this.editDialogVisible = true;
      this.editForm = Object.assign({}, row); //将数据传入dialog页面
      this.editForm.index = index;
    },
    // 取消修改按钮
    editPostCancel(valFalse) {
      this.editDialogVisible = valFalse;
    },
    // 确认修改按钮
    editAddPost(editForm, valFalse) {
      updatePost(this.editForm).then((res) => {
        if (res.status === 200) {
          this.getList();
          this.$message({
            message: "修改成功",
            type: "success",
          });
        }
      });
      this.editDialogVisible = valFalse;
    },
    // 删除按钮
    deletePost(rows) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletePost(rows.id).then((res) => {
            if (res.status === 200) {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-col {
  padding-left: 40px;
}
.el-button {
  margin-left: 100px;
}
.el-card {
  margin: 20px;
}
.el-pagination {
  float: right;
  margin: 20px;
}
</style>
